package com.itany.mall.dao.mapper;

import java.sql.ResultSet;

import com.itany.mall.entity.Order;
import com.itany.mall.entity.User;
import com.itany.mall.jdbc.template.callback.RowMapper;

public class OrderRowMapper implements RowMapper<Order> {

	public String selectSql() {
		StringBuffer sql = new StringBuffer();
		sql.append(" select ");
		sql.append("     o.id oid, ");
		sql.append("     o.order_num, ");
		sql.append("     o.create_time, ");
		sql.append("     o.price, ");
		sql.append("     u.id uid, ");
		sql.append("     u.name, ");
		sql.append("     u.password, ");
		sql.append("     u.phone, ");
		sql.append("     u.email, ");
		sql.append("     u.role ");
		sql.append(" from t_order o ");
		sql.append("     left join t_user u ");
		sql.append("         on o.user_id = u.id ");
		return sql.toString();
	}

	@Override
	public Order mapRow(ResultSet res) throws Exception {
		Order o = new Order();
		o.setId(res.getLong("oid"));
		o.setOrderNum(res.getString("order_num"));
		/*
		 ResultSet中的getDate()方法返回的是java.sql.Date
		 java.sql.Date是java.util.Date的子类
		 java.util.Date包含年月日时分秒, 可以精确到毫秒
		 java.sql.Date只包含年月日
		 createTime属性是java.util.Date
		 如果set一个java.sql.Date值, 会丢失时分秒信息
		 所以此处不能从ResultSet中getDate, 必须要get出包含时分秒的类型
		 ResultSet中的getTimestamp获取出的数据是携带年月日时分秒信息
		 */
		o.setCreateTime(res.getTimestamp("create_time"));
		o.setPrice(res.getDouble("price"));
		o.setUserId(res.getLong("uid"));
		User u = new User();
		u.setId(res.getLong("uid"));
		u.setName(res.getString("name"));
		u.setPassword(res.getString("password"));
		u.setPhone(res.getString("phone"));
		u.setEmail(res.getString("email"));
		u.setRole(res.getInt("role"));
		o.setUser(u);
		return o;
	}

}
